﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import './tuiguang.scss'
import Hearder from '../index/hearder'
import select01 from './image/select.png'
import select02 from './image/Selected.png'

export default class Index extends Component {  
  constructor(props) {   
    super(props);
    this.state = { 
       gwlist:[],
       nodate:'',
       shareId:''
    }
  }
  config = {
    navigationBarTitleText: '我要成为推广大使'
  }

  componentDidMount () { }
  componentWillMount () {
  	this.getgwlist()
  }

  componentDidShow () { }

  componentDidHide () { }
  getgwlist(){
  	var shopid = Taro.getStorageSync('shopId')
  	var url='/crm/papi/v1.9/consultant/find/'+shopid;
  	var _this=this
  	CommonJs.ajaxRequest(url,'','GET',function(res){
  		if(res.data&&res.data.length>0){
				_this.setState({
		      gwlist:res.data
		    })
			}
  		else{
  			_this.setState({
		      nodate:'店铺还未设置顾问，本店暂不支持推广',
		      gwlist:[]
		    })
  		}
  	},"wfw")
  }
  choose(id){
  	this.setState({
		   shareId:id  
		})
  }
  sure(){
		var url='/retail/papi/v1.9/sharer?shareId='+this.state.shareId
		console.log()
		CommonJs.ajaxRequest(url,'','POST',function(res){
			Taro.showModal({
				  title: '恭喜您成功成为本店的推广大使',
				  content: '是否前往浏览推广本店商品',
				  success: function (res) {
				    if (res.confirm) {
				      CommonJs.open('/pages/xijia/grouplist');
				    } 
				    else if (res.cancel) { 
				    	
				    }
				  }
				})
		},"wfw")
  }
  render () { 
    const list=this.state.gwlist
    return (
      <View className='page'>
       	<View className='tuiguang'>
       		<View className='tuiguan-tj'>
       			<View className='mobile'>
       				<Text className='tuiguan-tit'>成为推广大使流程</Text>
       				<Text className='tuiguan-li'>1.选择本店推广上级</Text>
       				<Text className='tuiguan-li'>2.在有分享佣金商品详情里分享商品</Text>
       				<Text className='tuiguan-li'>3.用户购买后获得佣金</Text>
       			</View>
       		</View>
       		<View className='tuiguan-list'>
       				<View className='mobile'>
       					<Text className='tuiguan-tit'>选择本店顾问作为上级</Text>
			        	<View className='zjlist myflex' style={list.length>0?'':'display:none'}>
			        		{list.map((item, index) => (  
				    			<View className={item.id==shareId?'zjli zjlic':'zjli'}  onClick={this.choose.bind(this,item.id)}>
				    				<View className='zjimg'><Image className="img" mode="scaleToFill" src={item.logo?item.logo:CommonJs.getlogo()}></Image></View>
				    				<View className='zjname'>{item.accountName}</View>
				    				<View className='zjchoose'>
				    					<Image className="img" mode="scaleToFill" src={item.id==shareId?select02:select01}></Image>
				    				</View>
				    			</View>
				    			))}
			        	</View>
			        	<View className='zjlist myflex' style={list.length==0?'':'display:none'}>
			        		<View className='nodate'>{this.state.nodate}</View> 
			        	</View>
			        </View>
       		</View>
       		<View className='btn01' onClick={this.sure.bind(this)}>确定提交</View>
       	</View>
      </View>
    )
  }
}

